|
|
 |
|
|
|
|
TIP Use constructors to initialize the data for an object. |
|
|
|
|
|
|
|
|
A destructor is a procedure thats automatically invoked when an object is destroyed. Destructors contain code that performs an objects cleanup. Each class created in Visual Basic has a Class_Terminate() routine as its destructor. |
|
|
|
|
|
|
|
|
When you remove a form from a Visual Basic project, all the memory and resources allocated to that form when it was created must now be deallocated. Again, a programmer had to write all the code to perform this deallocation when a form object is destroyed by Visual Basic. |
|
|
|
|
|
|
|
|
Lets take the example of an object that uses a file handle. You write the code to make sure that the file handle is closed and released back to the operating system before that reference to the object goes out of scope. The code to close the file handle is most likely contained within the destructor of that class. |
|
|
|
|
|
|
|
|
This is very similar to how the Data control works. If you place a Data control on a form it will open a recordset object for you. You dont need to ever close that recordset because the Data control does this for you. When the Data control is released from memory it calls the destructor method on the Recordset object which will close and release that object for you. |
|
|
|
 |
|
|
|
|
TIP Use Class_Terminate() to close file handles or perform other cleanup for an object. |
|
|
|
|
|
|
|
|
Object-based Coding Versus Procedural Coding |
|
|
|
|
|
|
|
|
Theres are big differences between procedural coding and object-oriented coding. The biggest one is how you deal with data in both methodologies. In the object-oriented paradigm, the data is passed in once to the object repository (properties) and then all routines (methods) work on this data without having to pass the data to each method. With procedural code, the data has to be passed to each function individually or be made global. Either way, you leave yourself wide open to mistakes. In procedural coding, youll generally see longer parameter lists than in object-oriented programming. |
|
|
|
|
|